//
//  LCResetPasswordVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/8.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCResetPasswordVC.h"
#import "LCUserModel.h"

@interface LCResetPasswordVC ()

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LCResetPasswordVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customTranslucentNavigationBar];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    LCLog(@"_registPhone = %@",_registPhone);
    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    bgImage.image = [UIImage imageNamed:@"publicBackground"];
    [self.view addSubview:bgImage];
    [self.view insertSubview:bgImage atIndex:0];
    
}
- (IBAction)registAccount:(id)sender {
    
    if (self.passwordTextField.text.length > 6) {
        self.registCode = @"888888";
        
        NSString *temp = @"e&VW$4#@Fk9)UC6b";
        LCLog(@"%@",temp);
        NSString *p1 = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:temp];
        LCLog(@"%@",p1);

        NSString *p2 = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:self.passwordTextField.text];
        LCLog(@"%@",p2);
        NSString *origin = [NSString stringWithFormat:@"%@%@",p1,p2];
        LCLog(@"%@",origin);
        NSString *password = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:origin];
        LCLog(@"%@",password);
        
        NSDictionary *parameter;
        NSString *requestURL = @"";
        switch (self.settingPasswordType) {
            case LCSettingPasswordTypeFirst:  //  首次设置密码
            {
                requestURL = @"/api/v0/add_user";
                NSDictionary *userDic = @{@"phone":self.registPhone,@"password":password};
                parameter = @{
                              @"sms_code":self.registCode,
                              @"user":userDic,
                              @"sms_token":[LCUserModel userModelManager].userid
                              };

            }
                break;
            case LCSettingPasswordTypeReset:  //  重置密码
            {
                requestURL = @"/api/v0/reset_password_sms";
                parameter = @{
                              @"phone":[LCUserModel userModelManager].mobile,
                              @"sms_code":self.registCode,
                              @"password":password
                              };
            }
                break;
                
            default:
                break;
        }
        
        
        
        
        [[LCNetwork shareInstance]lc_postNetworkWithRUL:requestURL parameter:parameter responseSuccess:^(id resultCode, id resultParameter) {
            
            if ([resultCode isEqualToString:@"0"]) {
                NSString *userID = [NSString stringWithFormat:@"%@",resultParameter[@"userid"]];
//                NSString *token = [NSString stringWithFormat:@"%@",resultParameter[@"token"]];
//                NSString *phone = [NSString stringWithFormat:@"%@",resultParameter[@"phone"]];
//                [LCUserModel userModelManager].userid = token;
//
                [LCUserModel userModelManager].userid = userID;
//
//                UIViewController *tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"LCTabBarVC"];
//
//                [UIApplication sharedApplication].keyWindow.rootViewController = tabBar;
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                LCLog(@"注册失败");
                [self showHint:resultParameter];
                
            }
            if ([requestURL rangeOfString:@"reset_password_sms"].location != NSNotFound) {
                
                [self showHint:@"密码修改成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
            
        } responseFail:^(id resultCode, NSError *error) {
            
        }];

    }else{
        
        [self showHint:@"请设置至少6位密码"];
    }
}

- (IBAction)textFieldSectury:(id)sender {
    
    self.passwordTextField.secureTextEntry = !self.passwordTextField.secureTextEntry;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
